
import numpy as np
import pylab as pl
from scipy.fftpack import fft, ifft,fftfreq
from signal_function import frame_fft, frame_ifft, frame_ifft_LPF, frame_reconstruct, framing, read_audio, emphasis,runing_average,zero_crossing_rate



filename = "test1.wav"
signal, time, sample_rate,num_frames = read_audio(filename)

emphasized_signal = emphasis(signal,0.95)
averaged_signal = runing_average(signal,10)

frame_stride = 0.01 # 24-10 = 14ms overlap 
frame_size = 0.024 #23ms frame length

frame_length = round(frame_size * sample_rate) # length of each frame 1058
frame_step = round(frame_stride * sample_rate) # strid length of each frame 441

signal_frame = framing(signal, frame_size,frame_stride,sample_rate,num_frames)
signal_frame = np.array(signal_frame)

window_frame = signal_frame * np.hamming(frame_length)

time_frame = framing(time,frame_size,frame_stride,sample_rate,num_frames)
time_frame = np.array(time_frame)

# average_frame = framing(averaged_signal,frame_size,frame_stride,sample_rate,num_frames)
# average_frame = np.array(average_frame)

# emp_frame = framing(emphasized_signal,frame_size,frame_stride,sample_rate,num_frames)
# emp_frame = np.array(emp_frame)


# determine frequency bands
noise_fre = 2000

################  FFT after framing  ############### 
FFT_frame = frame_fft(signal_frame)
winfowing_FFT_frame = frame_fft(window_frame)

frame_freq_band = fftfreq(frame_length, d= (1/ sample_rate))# frequency bands in each frame
freq_band = fftfreq(len(signal),d = (1/sample_rate)) #frequency band in whole signal

filtered_frame = frame_ifft_LPF(FFT_frame,frame_freq_band,noise_fre)#IFFT filtered signal frames
windowing_filtered_frame = frame_ifft_LPF(winfowing_FFT_frame,frame_freq_band,noise_fre) #IFFT filtered windowing frames


#reconstruct the IFFT frames
recons_filtered_signal = frame_reconstruct(filtered_frame,frame_step)
recons_windowing_filtered_signal = frame_reconstruct(windowing_filtered_frame,frame_step)

filterd_spectrum = fft(recons_filtered_signal) ### 
windowing_filtered_spectrum = fft(recons_windowing_filtered_signal)

filted_freq_band = fftfreq(len(recons_filtered_signal),d=(1/sample_rate))

zcr_filtred = zero_crossing_rate(filtered_frame,0)
zcr_windowing_filtered = zero_crossing_rate(windowing_filtered_frame,0)
zcr_original = zero_crossing_rate(signal_frame,0)

pl.subplot(312)
pl.title("ZCR: filtered vs windowing_filtered delta = 0")
pl.plot(zcr_filtred,label = "zcr_filtred")
pl.plot(zcr_windowing_filtered, label = "zcr_windowing_filtered")
pl.plot(zcr_original,label = "zcr_original")
pl.legend()

pl.subplot(311)
pl.title("overview signal: original vs filtered vs windowing_filtered")
pl.plot(signal,label ="original signal")
pl.plot(recons_filtered_signal, label = "recons_filtered_signal")
pl.plot(recons_windowing_filtered_signal, label = "recons_windowing_filtered_signal")
pl.legend()

pl.subplot(313)
pl.title("overview spectrum: original vs recons_filtered vs recons_windowing_filtered")
pl.plot(freq_band,fft(signal),label = "original spectrum")
pl.plot(freq_band[0:len(filterd_spectrum)], filterd_spectrum,label = "recons_filtered spectrum")
pl.plot(freq_band[0:len(windowing_filtered_spectrum)], windowing_filtered_spectrum,label = "recons_windowing_filtered spectrum")
pl.legend()

pl.show()

